package com.dragon.modules.yuegou.model.coupon;

import com.dragon.modules.yuegou.utils.BitCodeUtils;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 支持的获取优惠券通道（把数字看为二进制序列，从右往左数，第0位表示：购买、第1位表示：积分兑换、第2位表示：小票兑换）
 *
 * @author tangan
 *
 */
public class ChannelCode {

    private static final int BUY_IDX = 0;
    private static final int POINTS_IDX = 1;
    private static final int TICKETS_IDX = 2;

    private int code;

    public ChannelCode(int code) {
        super();
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    @JsonProperty("buy")
    public boolean isBuy() {
        return BitCodeUtils.get(code, BUY_IDX);
    }

    public void setBuy(boolean bit) {
        code = BitCodeUtils.set(code, BUY_IDX, bit);
    }

    @JsonProperty("points")
    public boolean isPoints() {
        return BitCodeUtils.get(code, POINTS_IDX);
    }

    public void setPoints(boolean bit) {
        code = BitCodeUtils.set(code, POINTS_IDX, bit);
    }

    public boolean isTickets() {
        return BitCodeUtils.get(code, TICKETS_IDX);
    }

    public void setTickets(boolean bit) {
        code = BitCodeUtils.set(code, TICKETS_IDX, bit);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ChannelCode [code=");
        builder.append(Integer.toBinaryString(code));
        builder.append(", isBuy()=");
        builder.append(isBuy());
        builder.append(", isPoints()=");
        builder.append(isPoints());
        builder.append(", isTickets()=");
        builder.append(isTickets());
        builder.append("]");
        return builder.toString();
    }
}
